package com.data.web.handlerTcpData;

import com.data.biz.domain.AccData;
import com.data.biz.domain.DipData;
import com.data.biz.mapper.AccDataMapperEx;
import com.data.biz.mapper.DipDataMapperEx;
import com.data.biz.utils.HexUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
@ChannelHandler.Sharable
@Slf4j
public class TcpDecoderHandler extends MessageToMessageDecoder {


    @Autowired
    private DataService dataService;


    @Override
    protected void decode(ChannelHandlerContext ctx, Object msg, List out) throws Exception {

        ByteBuf byteBuf = (ByteBuf) msg;
        byte[] originData = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(originData);
        log.info(originData.toString());
        log.info(String.valueOf(originData));
        String hexString = HexUtils.getBufHexStr(originData);
        String data = HexUtils.hexStringToString(hexString);
        log.info("线程111:" + Thread.currentThread().getName() + ":" + "hex转数据:" + data);

        if (data.length() < 100) {
            List<String> list = Arrays.asList(data.split(","));
            String ip = "";
            String type = "";
            String channel = "";
            for (String word : list) {
                if (word.contains("ip=")) {
                    ip = word;
                } else if (word.contains("type=")) {
                    type = word;
                } else if (word.contains("channel=")) {
                    channel = word;
                }
            }
            ip = ip.replaceAll("\\.", "_").replaceAll("ip=", "").trim();
            type = type.replaceAll("type=", "").trim();
            channel = channel.replaceAll("channel=", "").trim().toLowerCase(Locale.ROOT);
            String tableName = ip + "_" + type + "_" + channel;
//            if (type.equals("acc")) {
//                List<AccData> dataList = accDataMapperEx.selectLastData(tableName.trim());
//                if(dataList.size() == 0){
//                    AccData accData = new AccData();
//                    accData.setId(0);
//                    String command = "id=0" + "\n";
//                    log.info("发送了加速度读取命令:" + tableName + ",id:" + accData.getId());
//                    ctx.writeAndFlush(Unpooled.copiedBuffer(command, CharsetUtil.UTF_8));
//                }else {
//                    AccData accData = dataList.get(0);
//                    String command = "id=" +accData.getId() + "\n";
//                    log.info("发送了加速度读取命令:" + tableName + ",id:" + accData.getId());
//                    ctx.writeAndFlush(Unpooled.copiedBuffer(command, CharsetUtil.UTF_8));
//
//                }
//            }
//            if (type.equals("dip")) {
//                List<DipData> dataList = dipDataMapperEx.selectLastData(tableName.trim());
//                if(dataList.size() == 0){
//                    DipData dipData = new DipData();
//                    dipData.setId(0);
//                    String command = "id=0" + "\n";
//                    log.info("发送了倾角读取命令:" + tableName + ",id:" + dipData.getId());
//                    ctx.writeAndFlush(Unpooled.copiedBuffer(command, CharsetUtil.UTF_8));
//                }else {
//                    DipData dipData = dataList.get(0);
//                    String command = "id=" +dipData.getId() + "\n";
//                    log.info("发送了倾角读取命令:" + tableName + ",id:" + dipData.getId());
//                    ctx.writeAndFlush(Unpooled.copiedBuffer(command, CharsetUtil.UTF_8));
//
//                }
//            }
        }

//        dataService.handlerData(data);
    }


    private static void getTimeZone() {
        String dateStr = "fri feb 19 17:32:34 2021";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.US);
        Date date = new Date();
        try {
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String formatStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        System.err.println(formatStr);
    }


}
